package com.ylw.project.compile;

import com.ylw.project.Tools;
import org.apache.log4j.Logger;

import javax.tools.*;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 编译工具类
 */
public class CompileTools {
    private static final Logger logger = Logger.getLogger(CompileTools.class);

    /**
     * 编译 这里目前只需要这两个参数
     * @param needCompileFile 需要编译的文件
     * @param classPath
     * @param destinationPath  编译后的参数
     * @return
     */
    public static boolean compile(List<String> needCompileFile,String classPath,String destinationPath)  {
        logger.info(needCompileFile.toString()+" classPath "+classPath+" destinationPath "+destinationPath);
        //get lib dir
        List<File> files = convert2File(needCompileFile);

        List options = new ArrayList();
//        options.add("-Xlint:unchecked");
        options.add("-nowarn");
        options.add("-cp");
        options.add(classPath);
        options.add("-d");
        options.add(destinationPath);



       logger.info(Thread.currentThread().getName()+" ----compile options--------  "+options);
       logger.info(Thread.currentThread().getName()+" ----compile files--------  "+needCompileFile);

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, Charset.forName("utf-8"));

//        List<File> dependencies = new ArrayList<File>();
//        dependencies.add(new File("/Users/away/work/hc/project/wap3.0/webcontext/WEB-INF/lib/SevenStarWeb15.jar"));
//        try {
//            fileManager.setLocation(StandardLocation.CLASS_PATH, dependencies);
//        }catch (Exception e){
//            e.printStackTrace();
//        }
        Iterable<? extends JavaFileObject> compilationUnits1;
        compilationUnits1 = fileManager.getJavaFileObjectsFromFiles(files);

        compiler.getTask(null, fileManager, null, options, null, compilationUnits1).call();
        return true;
    }

    private static List<File> convert2File(List<String> filePath){
        List<File> result = new ArrayList();
        for (String temp :filePath){
            result.add(new File(temp));
        }
        return result;
    }


    /**
     * 获取该项目的lib路径
     * @param libDir
     * @return
     */
    public static String getAllJarPath(String libDir){
        File dir = new File(libDir);
        final StringBuilder sb = new StringBuilder();
        boolean flag = true;
        try{
            for(String name :dir.list()){
                if (name.endsWith(".jar")) {
                    if (flag) {
                        sb.append(dir.getPath() + File.separator + name);
                        flag = false;
                    } else {
                        if(Tools.isMacOs()) {
                            sb.append(":" + dir.getPath() + File.separator + name);
                        }else{
                            sb.append(";" + dir.getPath() + File.separator + name);
                        }
                    }
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return sb.toString();
    }

    public static void main(String[] args) {
        String[] ss = new String[]{"a","b"};
        System.out.println(Arrays.toString(ss));

        System.out.println(  getAllJarPath("I:\\java8\\jdk\\lib"));
    }
}
